package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Lesson;
import com.ruoyi.system.domain.LessonAttachment;
import com.ruoyi.system.domain.LessonStudent;
import com.ruoyi.system.domain.vo.UserVo;
import com.ruoyi.system.mapper.LessonAttachmentMapper;
import com.ruoyi.system.mapper.LessonMapper;
import com.ruoyi.system.mapper.LessonStudentMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.LessonService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程信息关联表(Lesson)表服务实现类
 *
 * @author makejava
 * @since 2024-05-21 12:04:40
 */
@Service("lessonService")
public class LessonServiceImpl implements LessonService {
    @Resource
    private LessonMapper lessonMapper;

    @Resource
    private LessonStudentMapper lessonStudentMapper;

    @Resource
    private LessonAttachmentMapper lessonAttachmentMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param lessonId 主键
     * @return 实例对象
     */
    @Override
    public Lesson queryById(Long lessonId) {
        Lesson lesson = lessonMapper.queryById(lessonId);
//        lesson.setAttachmentIds(lessonAttachmentMapper.selectAttachmentIdByLesson(lessonId));
//        lesson.setStudentIds(lessonStudentMapper.selectStudentIdByLesson(lessonId));
        return lesson;
    }

    /**
     * 分页查询
     *
     * @param lesson      筛选条件
     * @return 查询结果
     */
    @Override
    public List<Lesson> queryAll(Lesson lesson) {

        return lessonMapper.queryAll(lesson);
    }

    /**
     * 新增数据
     *
     * @param lesson 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Lesson insert(Lesson lesson) {
        this.lessonMapper.insert(lesson);
        insertLessonStudent(lesson);
        insertLessonAttachment(lesson);
        return lesson;
    }

    public void insertLessonAttachment(Lesson lesson) {
//        Long[] attachmentIds = lesson.getAttachmentIds();
//        if(StringUtils.isNotEmpty(attachmentIds)){
//            // 新增课程关联附件管理
//            ArrayList<LessonAttachment> list = new ArrayList<>(attachmentIds.length);
//            for (Long attachmentId : attachmentIds){
//                LessonAttachment la = new LessonAttachment();
//                la.setLessonId(lesson.getLessonId());
//                la.setAttachmentId(attachmentId);
//                list.add(la);
//            }
//            lessonAttachmentMapper.insertBatch(list);
//        }
    }

    public void insertLessonStudent(Lesson lesson) {
//        Long[] students = lesson.getStudentIds();
//        if (StringUtils.isNotEmpty(students))
//        {
//            // 新增课程关联学生管理
//            List<LessonStudent> list = new ArrayList<LessonStudent>(students.length);
//            for (Long studentId : students)
//            {
//                LessonStudent ls = new LessonStudent();
//                ls.setLessonId(lesson.getLessonId());
//                ls.setStudentId(studentId);
//                list.add(ls);
//            }
//            lessonStudentMapper.insertBatch(list);
//        }
    }

    /**
     * 修改数据
     *
     * @param lesson 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Lesson update(Lesson lesson) {
        lessonAttachmentMapper.deleteLessonAttachmentByLessonId(lesson.getLessonId());
        insertLessonAttachment(lesson);
        lessonStudentMapper.deleteLessonStudemtByLessonId(lesson.getLessonId());
        insertLessonStudent(lesson);
        this.lessonMapper.update(lesson);
        return this.queryById(lesson.getLessonId());
    }

    /**
     * 通过主键删除数据
     *
     * @param lessonId 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(Long lessonId) {
        lessonAttachmentMapper.deleteLessonAttachmentByLessonId(lessonId);
        lessonStudentMapper.deleteLessonStudemtByLessonId(lessonId);
        return this.lessonMapper.deleteById(lessonId) > 0;
    }

    /**
     * 通过班级id查询该班级学生学号
     * @param deptId
     * @return
     */
    @Override
    public List<UserVo> queryStudentByDept(Long deptId) {
        return sysUserMapper.queryStudentByDept(deptId);
    }
}
